﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
using System.Data.SqlClient;

namespace IHISCETeim.DAL
{
    public  class SPapersRemindDAL
    {

        /// <summary>
        /// 新增证件信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Add(SPapersRemindInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();


            DbCommand dbCommand = DB.GetStoredProcCommand("SPapersRemind_Add");
            DB.AddInParameter(dbCommand, "IDNO", DbType.String, obj.IDNO);
            DB.AddInParameter(dbCommand, "IDName", DbType.String, obj.IDName);
            #region
            SqlParameter RushDate = new SqlParameter("@RushDate", SqlDbType.DateTime);
            RushDate.IsNullable = true;
            if (obj.RushDate == DateTime.MinValue)
                RushDate.Value = DBNull.Value;
            else
                RushDate.Value = obj.RushDate;
            dbCommand.Parameters.Add(RushDate);

            SqlParameter YearCarefulDate = new SqlParameter("@YearCarefulDate", SqlDbType.DateTime);
            YearCarefulDate.IsNullable = true;
            if (obj.YearCarefulDate == DateTime.MinValue)
                YearCarefulDate.Value = DBNull.Value;
            else
                YearCarefulDate.Value = obj.YearCarefulDate;
            dbCommand.Parameters.Add(YearCarefulDate);

            SqlParameter ExpiryDate = new SqlParameter("@ExpiryDate", SqlDbType.DateTime);
            ExpiryDate.IsNullable = true;
            if (obj.ExpiryDate == DateTime.MinValue)
                ExpiryDate.Value = DBNull.Value;
            else
                ExpiryDate.Value = obj.ExpiryDate;
            dbCommand.Parameters.Add(ExpiryDate);
            #endregion
            DB.AddInParameter(dbCommand, "LeadDays", DbType.Int32, obj.LeadDays);
            DB.AddInParameter(dbCommand, "CycleDays", DbType.Int32, obj.CycleDays);
            DB.AddInParameter(dbCommand, "Department", DbType.String, obj.Department);
            DB.AddInParameter(dbCommand, "EmployeeName", DbType.String, obj.EmployeeName);
            DB.AddInParameter(dbCommand, "CreateUserId", DbType.Int32, obj.CreateUserId);
            DB.AddInParameter(dbCommand, "Licensedepartment", DbType.String, obj.Licensedepartment);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        /// <summary>
        /// 删除证件信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static bool Delete(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SPapersRemind_Delete", ID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 根据  ID获取相关证件信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static IDataReader GetByID(int ID)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("SPapersRemind_GetByID", ID);
            return DB.ExecuteReader(dbCommand);
	    }
        /// <summary>
        /// 获取每天需提醒的证件列表
        /// </summary>
        /// <returns></returns>
        public static IDataReader GetDayNeedRemind()
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("SPapersRemind_GetDayNeedRemind");
            return DB.ExecuteReader(dbCommand);
	    }
        /// <summary>
        /// 获取月头一天需提醒的证件列表
        /// </summary>
        /// <returns></returns>
        public static IDataReader GetMonthNeedRemind()
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("SPapersRemind_GetMonthNeedRemind");
            return DB.ExecuteReader(dbCommand);
	    }

        public static DataTable GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("SPapersRemind_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String,WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
			DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt32(DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool Update(SPapersRemindInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("SPapersRemind_Update");
            DB.AddInParameter(dbCommand, "ID", DbType.String, obj.ID);
            DB.AddInParameter(dbCommand, "IDNO", DbType.String, obj.IDNO);
            DB.AddInParameter(dbCommand, "IDName", DbType.String, obj.IDName);
            #region
            SqlParameter RushDate = new SqlParameter("@RushDate", SqlDbType.DateTime);
            RushDate.IsNullable = true;
            if (obj.RushDate == DateTime.MinValue)
                RushDate.Value = DBNull.Value;
            else
                RushDate.Value = obj.RushDate;
            dbCommand.Parameters.Add(RushDate);

            SqlParameter YearCarefulDate = new SqlParameter("@YearCarefulDate", SqlDbType.DateTime);
            YearCarefulDate.IsNullable = true;
            if (obj.YearCarefulDate == DateTime.MinValue)
                YearCarefulDate.Value = DBNull.Value;
            else
                YearCarefulDate.Value = obj.YearCarefulDate;
            dbCommand.Parameters.Add(YearCarefulDate);

            SqlParameter ExpiryDate = new SqlParameter("@ExpiryDate", SqlDbType.DateTime);
            ExpiryDate.IsNullable = true;
            if (obj.ExpiryDate == DateTime.MinValue)
                ExpiryDate.Value = DBNull.Value;
            else
                ExpiryDate.Value = obj.ExpiryDate;
            dbCommand.Parameters.Add(ExpiryDate);
            #endregion
            DB.AddInParameter(dbCommand, "LeadDays", DbType.Int32, obj.LeadDays);
            DB.AddInParameter(dbCommand, "CycleDays", DbType.Int32, obj.CycleDays);
            DB.AddInParameter(dbCommand, "Department", DbType.String, obj.Department);
            DB.AddInParameter(dbCommand, "EmployeeName", DbType.String, obj.EmployeeName);
            DB.AddInParameter(dbCommand, "Licensedepartment", DbType.String, obj.Licensedepartment);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
    }
}
